# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :robot_room.py
# @Time      :2022/7/28 14:57
# @Author    :吕华龙
import time
import random
from flask import render_template, Blueprint, request
from flask_socketio import SocketIO, emit, join_room, leave_room

name_space = '/name_space'

robot_room_bp = Blueprint("robot_room", __name__, url_prefix="/robot")
socketio = SocketIO()


@robot_room_bp.route('/')
def test_sconnect():
    return render_template("index_room.html")


@socketio.on('connect', namespace=name_space)
def test_connect():
    print('1111')


@socketio.on('joinRoom', namespace=name_space)
def post_connect(message):
    join_room(message['room'])
    emit("roomJoined", {
        "user": request.sid,
        "room": message['room']
    }, to=message['room'])


@socketio.on('leaveRoom', namespace=name_space)
def cick_connect(message):
    emit("roomLeftPersonal", {"room": message['room']})
    leave_room(message['room'])
    emit("roomLeft", {"user": request.sid, "room": message['room']}, to=message['room'])


@socketio.on('sendMsg', namespace=name_space)
def send_message(message):
    emit('SendtoAll', {'msg': message['msg'], 'user': request.sid}, to=message['room'])
#
# @socketio.on('disconnect', namespace="/dcenter")
# def test_disconnect():
#     print('Client disconnected')

#
# @socketio.on('message', namespace="/dcenter")
# def handle_message(data):
#     room_id = data.get('room')
#
#     socketio.emit(room_id, {'data': data.get('message')}, namespace="/dcenter")
#
